package com.tyc.thread;

import java.util.concurrent.*;

/**
 * 线程创建方式
 */
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        CountDownLatch countDownLatch = new CountDownLatch(4);
        class Task1 extends Thread{
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"task1");
                countDownLatch.countDown();
            }
        }
        // 方式1：继承 Thread 类
        new Task1().start();
        // 方式2：实现 runnable 接口
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"task3");
            countDownLatch.countDown();
        }).start();
        // 方式3：利用线程池
        executor.execute(()->{
            System.out.println(Thread.currentThread().getName()+"task4");
            countDownLatch.countDown();
        });
        // 方式4：利用futureTask 实际还是 runnable 只不过利用futureTask获取返回结果
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + "task2");
            countDownLatch.countDown();
            return "success";
        });
        new Thread(futureTask).start();
        countDownLatch.await();
        System.out.println("over");
        executor.shutdownNow();
        Executors.newSingleThreadExecutor();
        Executors.newFixedThreadPool(2);
        Executors.newCachedThreadPool();
        Executors.newScheduledThreadPool(2);
        Executors.newWorkStealingPool(2);

    }
}
